<#-- Includes -->
<#include "../auxftl/constants.ftl">
<#include "../auxftl/propertyutils.ftl">
<#-- Global lists -->
<#assign propertyList = createPropertyList()/>
<#assign dateTimeList = createDateTimePropertyList()/>
<#-- Package and imports  -->
package ${controllerPackage};

<#if pojo.packageName != "">
import ${pojo.packageName}.*;
</#if>
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
<#if (dateTimeList?size > 0)>
import java.util.Date;
</#if>
import java.util.List;
import java.util.ArrayList;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

/**
 * Class which represents list of ${entityName}
 * created: ${date?string("dd.MM.yyyy HH:mm:ss")}
 */
<#assign idName = pojo.identifierProperty.name>
<#assign idTypeName = pojo.identifierProperty.type.returnedClass.name>
public class ${entityName}List extends HibernateList<${entityName}, ${idTypeName}> implements ActionBean {

<#-- Property declaration  -->
  protected ActionBeanContext context;
<#if pojo.isComponent(pojo.identifierProperty)>
  protected ${entityName} ${componentName};
<#else>
  protected ${entityName} ${componentName} = new ${entityName}();
</#if>
<#foreach prop in dateTimeList>
  protected Date ${prop.name}From;
  protected Date ${prop.name}To;
</#foreach>

  /**
   * Constructor, initialization of list
   */
  public ${entityName}List() {
    super();
<#if pojo.isComponent(pojo.identifierProperty)>

    // Creates search instanstance
    ${componentName} = new ${entityName}();
    ${componentName}.setId(new ${entityName}Id());
</#if>

    // Sests params
    setDao(new ${entityName}DAO());
    setCriterions(createCriterions());
    setMaxPageResults(25);
  }

  /**
   * Creates criteria list for filter
   *
   * @return criterions
   */
  public Criterion[] createCriterions() {

    // Creates filter criteria
    List<Criterion> crits = new ArrayList<Criterion>();
<#foreach prop in propertyList>
<#assign propCap = prop.name?cap_first>
<#if pojo.isComponent(prop)>
<#foreach cp in prop.value.propertyIterator>
<#assign cpCap = cp.name?cap_first>
<#if isString(cp)>
    if (${componentName}.get${propCap}().get${cpCap}() != null) {
      crits.add(Restrictions.like("${prop.name}.${cp.name}", ${componentName}.get${propCap}().get${cpCap}() + "%"));
    }
<#elseif isDate(cp) || isTimestamp(cp) || isTime(cp)>
    if (${cp.name}From != null) {
      crits.add(Restrictions.ge("${prop.name}.${cp.name}", ${cp.name}From));
    }
    if (${cp.name}To != null) {
      crits.add(Restrictions.le("${prop.name}.${cp.name}", ${cp.name}To));
    }
</#if>
</#foreach>
<#else>
<#if isString(prop)>
    if (${componentName}.get${propCap}() != null) {
      crits.add(Restrictions.like("${prop.name}", ${componentName}.get${propCap}() + "%"));
    }
<#elseif isDate(prop) || isTimestamp(prop) || isTime(prop)>
    if (${prop.name}From != null) {
      crits.add(Restrictions.ge("${prop.name}", ${prop.name}From));
    }
    if (${prop.name}To != null) {
      crits.add(Restrictions.le("${prop.name}", ${prop.name}To));
    }
</#if>
</#if>
</#foreach>
    return crits.toArray(new Criterion[crits.size()]);
  }

  /**
   * Returns entity instance (used for filters)
   *
   * @return ${entityName} instance
   */
  public ${entityName} get${entityName}() {
    return ${componentName};
  }
<#-- Setters and getters -->
<#foreach prop in dateTimeList>
<#assign upperPropName = prop.name?cap_first>

  /**
   * Returns starting date for ${prop.name}
   *
   * @return starting date for ${prop.name}
   */
  public Date get${upperPropName}From() {
    return ${prop.name}From;
  }

  /**
   * Returns starting date for ${prop.name} as string
   *
   * @return starting date for ${prop.name} as string
   */
  public String get${upperPropName}StrFrom() {
    return ${prop.name}From == null ? null : StringUtils.dateToStr(${prop.name}From);
  }

  /**
   * Sets starting date for ${prop.name}
   *
   * @param ${prop.name}From starting date for ${prop.name}
   */
  public void set${upperPropName}From(Date ${prop.name}From) {
    this.${prop.name}From = ${prop.name}From;
  }

  /**
   * Sets string starting date for ${prop.name}
   *
   * @param ${prop.name}StrFrom starting string date for ${prop.name}
   */
  public void set${upperPropName}StrFrom(String ${prop.name}StrFrom) {
    if (${prop.name}StrFrom == null || ${prop.name}StrFrom.equals("")) {
      this.set${upperPropName}From(null);
      return;
    }
    try {
      java.util.Date tmp = StringUtils.strToDate(${prop.name}StrFrom);
      this.set${upperPropName}From(tmp);
    } catch(Exception e) {
      System.out.println(e.getMessage());
    }
  }

  /**
   * Returns ending date for ${prop.name}
   *
   * @return ending date for ${prop.name}
   */
  public Date get${upperPropName}To() {
    return ${prop.name}To;
  }

  /**
   * Returns ending date for ${prop.name} as string
   *
   * @return ending date for ${prop.name} as string
   */
  public String get${upperPropName}StrTo() {
    return ${prop.name}To == null ? null : StringUtils.dateToStr(${prop.name}To);
  }

  /**
   * Sets ending date for ${prop.name}
   *
   * @param ${prop.name}To ending date for ${prop.name}
   */
  public void set${upperPropName}To(Date ${prop.name}To) {
    this.${prop.name}To = ${prop.name}To;
  }

  /**
   * Sets string ending date for ${prop.name}
   *
   * @param ${prop.name}StrTo ending string date for ${prop.name}
   */
  public void set${upperPropName}StrTo(String ${prop.name}StrTo) {
    if (${prop.name}StrTo == null || ${prop.name}StrTo.equals("")) {
      this.set${upperPropName}To(null);
      return;
    }
    try {
      java.util.Date tmp = StringUtils.strToDate(${prop.name}StrTo);
      this.set${upperPropName}To(tmp);
    } catch(Exception e) {
      System.out.println(e.getMessage());
    }
  }
</#foreach>

  /**
   * Sets context
   *
   * @param context action bean context
   */
  public void setContext(ActionBeanContext context) {
    this.context = context;
  }

  /**
   * Returns context
   *
   * @return context
   */
  public ActionBeanContext getContext() {
    return this.context;
  }

  /**
   * Applies filter on result list
   *
   * @return resolution
   * @throws java.io.IOException exception
   */
  public Resolution doFilter() throws java.io.IOException {

    // Apply filter criteria
    setCriterions(createCriterions());

    // Forward to action
    return new ForwardResolution("/${entityName}List.jsp");
  }
}
